<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-form">
        <h3>用户注册</h3>
        <el-form ref="form" :model="user" :rules="rules" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="user.gender">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="user.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item class="button-container">
            <el-button type="primary" @click="submitForm" :loading="loading">提交</el-button>
          </el-form-item>
          <el-form-item class="button-container login-button-container">
            <router-link to="/login">
              <el-button class="login-button" type="text">已有账号？去登录</el-button>
            </router-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user.js'

export default {
  name: 'Register',
  data() {
    return {
      user: {
        username: '',
        gender: '',
        phone: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    async submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await register(this.user)
            this.$router.push('/login')
          } catch (error) {
            if (error.response && error.response.status === 409) {
              this.$message.error('用户名已存在')
            } else {
              this.$message.error('注册失败')
            }
            this.loading = false
          }
        } else {
          console.log('form validation failed')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.register-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .register-box {
    width: 400px;
    border-radius: 5px;
    box-shadow: 0px 0px 10px #ccc;

    .register-form {
      padding: 20px;

      h3 {
        font-size: 24px;
        margin-bottom: 20px;
        text-align: center;
      }

      .button-container {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
      }

      .login-button-container {
        margin-top: 20px; /* 增加两个按钮之间的间距 */
      }

      .login-button {
        color: #409EFF;
        opacity: 0.75; /* 设置按钮的透明度 */
        &:hover {
          opacity: 1; /* 悬停时不透明 */
        }
      }
    }
  }
}
</style>
